package com.hna.eking.AirGisPlt.Utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GetAreaWetherUtils {

	String RetJsonString;
	String Url = "https://way.jd.com/FourDSmart/WeatherLive";
	CloseableHttpClient client;
	CloseableHttpResponse response;
	InputStream is = null;  
	private static final Logger logger = LoggerFactory.getLogger(GetAreaWetherUtils.class);
	
	public GetAreaWetherUtils(){
		
	}
	
	public String GetWetherInfo(double PosX, double PosY){
		String RetStr = "";
		try {
			client = HttpClientBuilder.create().build();  
			RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(50).setConnectTimeout(50)  
				    .setSocketTimeout(50).build();
			List<NameValuePair> params = new ArrayList();  
		    params.add(new BasicNameValuePair("longitude", String.format("%f", PosX)));  
		    params.add(new BasicNameValuePair("latitude", String.format("%f", PosY)));  
		    params.add(new BasicNameValuePair("appkey", "adfe378ef9cc8a216c7690983403345e"));  
			String Str = EntityUtils.toString(new UrlEncodedFormEntity(params,Consts.UTF_8));  
			HttpGet httpget = new HttpGet(Url + "?" + Str);
			response = client.execute(httpget);
			HttpEntity entity  = response.getEntity();  
			if(entity != null){  
                is = entity.getContent();  
                //转换为字节输入流  
                BufferedReader br = new BufferedReader(new InputStreamReader(is, Consts.UTF_8));  
                String body = null;  
                while((body=br.readLine()) != null){  
                	logger.info(body);
                	RetStr += body;  
                }  
            } 
			//logger.info("return:" + RetStr);
		} catch (IOException e) {;
			// TODO Auto-generated catch block
			e.printStackTrace();
		}  finally{
			try {
				response.close();
				client.close();
				return RetStr;
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		return null;
		
	}
}
